 /*
 ** KF-Ray, raytracer parallèle
 **  arguments.c
 **
 ** Copyright 2009 Karin Ait-Si-Amer & Florian Dang
 ** <aitsiame@polytech.upmc.fr>
 ** <tdang@polytech.upmc.fr>
 **
 */

 /*************************************************************************
 *   This file is part of KF-Ray.
 *
 *   KF-Ray is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   KF-Ray is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with KF-Ray.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string.h>

#include "main.h"
#include "misc/printinfo.h"
#include "arguments.h"

t_arguments	CreateArg	(char *scn_name0, char *img_name0,
				int *model_brdf0, int *model_texture0,
				int *n_lines0, int *opt_display0,
				int *opt_aliasing0, float *cam_move0,
				int *opt_mist0)
{
	t_arguments arg_main;

	arg_main.scn_name = scn_name0;
	arg_main.img_name = img_name0;
	arg_main.model_brdf = model_brdf0;
	arg_main.model_texture = model_texture0;
	arg_main.n_lines = n_lines0;
	arg_main.opt_display = opt_display0;
	arg_main.opt_aliasing = opt_aliasing0;
	arg_main.cam_move = cam_move0;
	arg_main.opt_mist = opt_mist0;

	return	arg_main;
}


void		PrintArg(t_arguments arg_main)
{
	printf("scn: %s - img: %s - brdf/texture/n_lines: opt_display/opt_aliasing:",
		arg_main.scn_name, arg_main.img_name);
}


int		LookArguments(int argc, char **argv, t_arguments arg_main)
{
	int	optchar;

	// Rappel i: un argument h sans argument t:: argument optionnel
	while ((optchar = getopt (argc, argv, "i:o:h c b:t l:v:m:a d ")) != -1)
	{
		switch (optchar)
		{
			case 'i':
				OptScn(arg_main.scn_name, arg_main.img_name, optarg);
				break;

			case 'o':
				OptImg(arg_main.img_name, optarg);
				break;

			case 'h':
				return OptHelp();

			case 'c':
				return OptClean();

			case 'a':
				OptAliasing(arg_main.opt_aliasing);
				break;

			case 'd':
				OptDisplay(arg_main.opt_display);
				break;

			case 'b':
				if(!OptBrdf(arg_main.model_brdf, optarg))
					return OPT_STOP;
				break;

			case 't':
				OptTexture(arg_main.model_texture);
				break;

			case 'l':
				OptLines(arg_main.n_lines, optarg);
				break;

			case 'v':
				OptView(arg_main.cam_move, optarg);
				break;

			case 'm':
				OptMist(arg_main.opt_mist, optarg);
				break;

			case '?':
				return OptUnknown(argv[0]);

			default :
				break;
		}
	}

#ifndef MY_MPI
	printf("%s", splash);
	printf(">> Scene file		:	[%s]\n", arg_main.scn_name);
#endif

	return	 OPT_CONTINUE;
}



int		OptScn(char *scn_name, char *img_name, char *optarg)
{
	strcpy(scn_name, optarg);

	// On regarde si le nom finit par .kfr et s'il a un .
	if (strlen(scn_name) >= 3)
		if (
		strcmp(".kfr", scn_name + (strlen(scn_name)-4)) != 0 &&
		strchr(scn_name, '.') == NULL
		)
			strcat(scn_name, ".kfr");

	if (strlen(scn_name) < 3)
		strcat(scn_name, ".kfr");

	// On donne à l'image le nom de la scène par défaut
	strcpy(img_name, optarg);
	if (strlen(optarg) >= 3)
		if (strcmp(".kfr", scn_name + (strlen(scn_name)-4)) == 0)
			img_name[strlen(scn_name)-4] = '\0'; // On tronque

	strcat(img_name, ".ppm");

	return 	OPT_CONTINUE;
}


int		OptImg(char *img_name, char *optarg)
{
	strcpy(img_name, optarg);

	if (strlen(img_name) >= 3)
		if (strcmp(".ppm", img_name + (strlen(img_name)-4)) != 0)
			strcat(img_name, ".ppm");

	if (strlen(img_name) < 3)
		strcat(img_name, ".ppm");

	return 	OPT_CONTINUE;
}


int		OptHelp(void)
{
	printf(usage);
	printf(help);

	return OPT_STOP;
}


int		OptClean(void)
{
	char	*path;
	path = (char *)malloc(MAX_FILE_LENGTH * sizeof(char));

	sprintf(path, "rm ../scenes/images/*.ppm");

	execlp("/bin/sh", "/bin/sh", "-c", path, NULL);

	free(path);

	return	 OPT_STOP;
}


int		OptDisplay(int *opt_display)
{
	*opt_display = 1;

	return OPT_CONTINUE;
}


int		OptAliasing(int *opt_aliasing)
{
	*opt_aliasing = 1;

	return	OPT_CONTINUE;
}


int		OptBrdf(int *model_brdf, char *optarg)
{
	if (atoi(optarg) >= 0 && atoi(optarg) <= 3)
		*model_brdf = atoi(optarg);
	else
	{
		printf("-b option should have a number between 1 and 3\n");
		printf(usage);

		return OPT_STOP;
	}

	return	OPT_CONTINUE;
}


int		OptTexture(int *model_texture)
{
	*model_texture = 0;

	return	OPT_CONTINUE;
}


int		OptLines(int *n_lines, char *optarg)
{
	if (*n_lines > 0)
		*n_lines = atoi(optarg);
	else
	{
		printf("-l option should have a number between 1 and image height\n");
		printf(usage);

		return OPT_STOP;
	}

	return	OPT_CONTINUE;
}


int		OptView(float *cam_move, char *optarg)
{
	*cam_move = atof(optarg);

	return	OPT_CONTINUE;
}

int		OptMist(int *opt_mist, char *optarg)
{
	*opt_mist = atoi(optarg);

	return	OPT_CONTINUE;
}


int		OptUnknown(char *prgm_name)
{
	printf ("KF-Ray - Unknown commande...\n");
	printf(usage);
	printf ("For help type : %s -h\n", prgm_name);

	return	OPT_STOP;
}
